package handler

import (
	"database-platform/handler/common"
	"database-platform/logger"
	"database-platform/model"
	"database-platform/service"
	"fmt"
	"github.com/gin-gonic/gin"
)

// ListServerHandler 处理获取服务器列表的请求
func ListServerHandler(c *gin.Context) {
	params, err := getServerListParams(c)
	if err != nil {
		msg := fmt.Sprintf("ListServerHandler.error.message.%s", err.Error())
		logger.Logger.Error(msg)
		common.FailWithMsg(c, "参数解析错误")
		return
	}

	res, err := service.GetServerList(params)
	if err != nil {
		common.FailWithMsg(c, err.Error())
		return
	}
	common.OkWithData(c, res)
}

func getServerListParams(c *gin.Context) (params *model.ServerListRequest, err error) {
	params = &model.ServerListRequest{
		Hostname: c.DefaultQuery("hostname", ""),
		IP:       c.DefaultQuery("ip", ""),
		Env:      c.DefaultQuery("env", ""),
		DBType:   c.DefaultQuery("db_type", ""),
		Search:   c.DefaultQuery("search", ""),
	}
	params.Page, params.PageSize = common.GetPagePageSizeParams(c)
	return params, err
}

func RetrieveServerHandler(c *gin.Context) {
	id, err := common.GetIdParams(c)
	if err != nil {
		logger.Logger.Error(fmt.Sprintf("RetrieveServerHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, err.Error())
		return
	}
	result, err := service.GetServerById(id)
	if err != nil {
		msg := fmt.Sprintf("RetrieveServerHandler.error.message.%s", err.Error())
		logger.Logger.Error(msg)
		common.FailWithMsg(c, fmt.Sprintf("获取详情错误，id:%v, err:%s", id, err.Error()))
		return
	}
	common.OkWithData(c, result)
}

// InsertServerHandler 处理插入服务器记录的请求
func InsertServerHandler(c *gin.Context) {

	var server model.Server
	if err := c.ShouldBindJSON(&server); err != nil {
		logger.Logger.Error(fmt.Sprintf("InsertServerHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, "参数解析错误")
		return
	}

	err := service.InsertServer(&server)
	if err != nil {
		msg := fmt.Sprintf("InsertServerHandler.error.message.%s", err.Error())
		logger.Logger.Error(msg)
		common.FailWithMsg(c, err.Error())
		return
	}
	common.OkWithMsg(c, "插入成功")
}

// UpdateServerHandler 处理更新服务器记录的请求
func UpdateServerHandler(c *gin.Context) {
	id, err := common.GetIdParams(c)
	if err != nil {
		logger.Logger.Error(fmt.Sprintf("UpdateServerHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, err.Error())
		return
	}

	var server model.Server
	if err := c.ShouldBindJSON(&server); err != nil {
		logger.Logger.Error(fmt.Sprintf("UpdateServerHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, "参数解析错误")
		return
	}

	err = service.UpdateServer(id, &server)
	if err != nil {
		logger.Logger.Error(fmt.Sprintf("UpdateServerHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, err.Error())
		return
	}
	common.OkWithMsg(c, "更新成功")
}

// DeleteServerHandler 处理删除服务器记录的请求
func DeleteServerHandler(c *gin.Context) {
	id, err := common.GetIdParams(c)
	if err != nil {
		logger.Logger.Error(fmt.Sprintf("DeleteServerHandler.error.message.%s", err.Error()))
		common.FailWithMsg(c, err.Error())
		return
	}

	err = service.DeleteServer(id)
	if err != nil {
		msg := fmt.Sprintf("DeleteServerHandler.error.message.%s", err.Error())
		logger.Logger.Error(msg)
		common.FailWithMsg(c, err.Error())
		return
	}
	common.OkWithMsg(c, "删除成功")
}
